//
//  BookingHotelDateCCell.swift
//  DLuxTravel
//
//  Created by 陆涛 on 2018/12/3.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit
import JTAppleCalendar

enum DateBGType {
    
    case none
    case begin
    case center
    case end
}

class BookingHotelDateCCell: JTAppleCell {

    @IBOutlet weak var dateLabel: UILabel!
    
    @IBOutlet weak var bgView: UIView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        bgView.backgroundColor = kTitleColor
        bgView.isHidden = true
    }
    
    private var choosed:Bool = false {
        
        didSet {
            
            self.bgView.isHidden = !choosed
            
            if choosed {
                
                self.dateLabel.textColor = UIColor.white
            } else {
                
                if cellState?.dateBelongsTo == .thisMonth {
                    
                    self.dateLabel.textColor = kTitleColor
                } else {
                    
                    self.dateLabel.textColor = kContentColor
                }
            }
        }
    }
    
    func setupBG(type:DateBGType) {
        
        
        let width = (CGFloat(kScreenWidth) - 40) / 7
        
        choosed = true
        bgView.backgroundColor = kTitleColor
        
        switch type {
        case .begin:
            
            let roundedRect = CGRect(x: 0, y: 0, width: width, height:30)
            let rounded = UIBezierPath(roundedRect: roundedRect, byRoundingCorners: [.topLeft,.bottomLeft], cornerRadii: CGSize(width: 15, height: 15));
            
            let shape = CAShapeLayer()
            shape.path = rounded.cgPath
            
            
            bgView.layer.mask = shape
            
            self.dateLabel.text = "\(cellState?.text ?? "")\n入住"
            
        case .end:
            
            let roundedRect = CGRect(x: 0, y: 0, width: width, height:30)
            let rounded = UIBezierPath(roundedRect: roundedRect, byRoundingCorners: [.topRight,.bottomRight], cornerRadii: CGSize(width: 15, height: 15));
            
            let shape = CAShapeLayer()
            shape.path = rounded.cgPath
            
            
            bgView.layer.mask = shape
            
            self.dateLabel.text = "\(cellState?.text ?? "")\n离店"
            
        case .center:
            
            bgView.layer.mask = nil
            bgView.backgroundColor = kContentColor
            
        default:
            choosed = false
        }
    }
    
    var cellState: CellState? {
        
        didSet {
            
            self.dateLabel.text = cellState?.text
            
            if cellState?.dateBelongsTo == .thisMonth {
                
                self.dateLabel.textColor = kTitleColor
                self.isUserInteractionEnabled = true
            } else {
                
                self.dateLabel.textColor = kContentColor
                self.isUserInteractionEnabled = false
            }
        }
    }
}
